//
//  MenkSoftAlert.m
//  MenkSoftAlert
//
//  Created by Burn on 2019/4/24.
//  Copyright © 2019 menksoft. All rights reserved.
//

#define MenkTransform                    CGAffineTransformConcat(CGAffineTransformMakeRotation(M_PI_2), CGAffineTransformMake(-1, 0, 0, 1, 0, 0))
#define MenksoftQagan_mirror(x)          [UIFont fontWithName:@"MenksoftQagan_mirror" size:x] //蒙科立镜像字体
#define HvrStudioChaganTig_M(x)          [UIFont fontWithName:@"HvrStudioChaganTig-M" size:x] //标准编码使用
#define kSCOLOR [UIColor colorWithRed:21 / 255.0 green:126 / 255.0 blue:251 / 255.0 alpha:1]

//  iPhone_X
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IOS_11  ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.f)
#define IS_IPHONE_X (IS_IOS_11 && IS_IPHONE && (MIN([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) >= 375 && MAX([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) >= 812))

#define KSCALEH(h) h * [UIScreen mainScreen].bounds.size.height / 812
#define KSCALEW(w) w * [UIScreen mainScreen].bounds.size.width / 375

#define kW KSCALEW(170)
#define kH KSCALEH(280)
#define kBTNW 45
#define KBTNH kH * .5

#define SCREENW [UIScreen mainScreen].bounds.size.width
#define SCREENH [UIScreen mainScreen].bounds.size.height

#import "MenkSoftAlert.h"

@interface MenkSoftAlert ()

@property(strong,nonatomic) UILabel *backGroundL;
@property(strong,nonatomic) UILabel *message;
@property(strong,nonatomic) UILabel *topbtn;
@property(strong,nonatomic) UILabel *bobtn;
@property(strong,nonatomic) ClickCH chClick;

@property(strong,nonatomic) UILabel *shawdL;

@end


@implementation MenkSoftAlert

- (void)menkAlert:(NSString *)message topCH:(NSString *_Nullable )topCH bottonCH:( NSString * _Nullable )bCH click:(ClickCH)click{

    self.chClick = click;
    
    UIView *view = [UIApplication sharedApplication].keyWindow;
    self.shawdL = [[UILabel alloc]initWithFrame:view.bounds];
    self.shawdL.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:.5];
    [view addSubview:self.shawdL];
    
    self.frame = CGRectMake(0, 0, kW, kH);
    self.center = view.center;
    self.backGroundL = [[UILabel alloc]init];
    self.backGroundL.layer.cornerRadius = 15;
    self.backGroundL.layer.masksToBounds = YES;

    self.backGroundL.frame = self.bounds;
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = self.backGroundL.bounds;
    [self.backGroundL addSubview:effectView];
    
    [self addSubview:self.backGroundL];
    [view addSubview:self];
    
    
    self.message = [[UILabel alloc]init];
    self.message.text = message;
    self.message.font = HvrStudioChaganTig_M(16);
    self.message.numberOfLines = 0;
    self.message.textAlignment = NSTextAlignmentCenter;
    self.message.transform = MenkTransform;
    self.message.frame = CGRectMake(10, 10, kW - 10 - kBTNW, kH - 20);
    [self addSubview:self.message];
    
    self.topbtn = [[UILabel alloc]init];
    self.topbtn.text = topCH;
    self.topbtn.font = HvrStudioChaganTig_M(16);
    self.topbtn.numberOfLines = 0;
    self.topbtn.transform = MenkTransform;
    self.topbtn.userInteractionEnabled = YES;
    UITapGestureRecognizer *labelTapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chclick:)];
    [self.topbtn addGestureRecognizer:labelTapGestureRecognizer1];
    self.topbtn.frame = CGRectMake(kW - kBTNW, 5, kBTNW, kH * .5 - 5);
    self.topbtn.textColor = kSCOLOR;
    self.topbtn.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.topbtn];
    
    if (bCH.length > 0) {
        self.bobtn = [[UILabel alloc]init];
        self.bobtn.text = bCH;
        self.bobtn.font = HvrStudioChaganTig_M(16);
        self.bobtn.numberOfLines = 0;
        self.bobtn.transform = MenkTransform;
        self.bobtn.userInteractionEnabled = YES;
        self.bobtn.textAlignment = NSTextAlignmentCenter;
        UITapGestureRecognizer *labelTapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chclick:)];
        [self.bobtn addGestureRecognizer:labelTapGestureRecognizer1];
        self.bobtn.frame = CGRectMake(kW - kBTNW,kH - (kH * .5 - 5) , kBTNW, kH * .5 - 5);
        self.bobtn.textColor = kSCOLOR;
        [self addSubview:self.bobtn];
        
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(kW - kBTNW, kH * .5 - .5, kBTNW, 1);
        label.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:label];
    }else{
        self.topbtn.frame = CGRectMake(kW - kBTNW, 5, kBTNW, kH - 10);
    }
    
    UILabel *line = [[UILabel alloc]init];
    line.frame = CGRectMake(kW - kBTNW, 0, 1, kH);
    line.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:line];

    
    
//    UILongPressGestureRecognizer *longP = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longP:)];
//    longP.minimumPressDuration = .2;
//    [self.topbtn addGestureRecognizer:longP];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.transform = CGAffineTransformScale(self.transform, 0.9, 0.9);
    }];

}

- (void)chclick:(UIGestureRecognizer *)ges{
    UILabel *label = (UILabel *)ges.view;
    if ([self.topbtn isEqual:label]) {
        self.chClick(0);
    }else{
        self.chClick(1);
    }
    [self.shawdL removeFromSuperview];
    [self removeFromSuperview];
    
    
    
}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
//    NSLog(@"%f",point.x);
////    if ([self pointInside:point withEvent:self]) {
////        return self;
////    }else{
////        return nil;
////    }
////
//
//    point = [self.layer convertPoint:point fromLayer:self.layer];
//    if ([self.layer containsPoint:point]) {
//        //处理点击到这个view中要执行的事件
//        return self;
//    }else{
//        return nil;
//    }
//}


- (void)longP:(UIGestureRecognizer *)ges{
    switch (ges.state) {
        case UIGestureRecognizerStateBegan:
            ges.view.backgroundColor = [UIColor lightGrayColor];
            break;
        case UIGestureRecognizerStateEnded:
            ges.view.backgroundColor = [UIColor clearColor];
            break;
            
        default:
            break;
    }
}


- (void)menkAlertB:(NSString *)message chooss:(NSArray <NSString *>*)chooss click:(ClickCH)click{
    self.chClick = click;
    
    //  高度 280
    CGFloat AH = 80 + chooss.count * 100;
    //  上部分b高度 200
    CGFloat TH = chooss.count * 100;
    
    UIView *view = [UIApplication sharedApplication].keyWindow;
    self.shawdL = [[UILabel alloc]initWithFrame:view.bounds];
    self.shawdL.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:.5];
    [view addSubview:self.shawdL];
    
    
    
    self.frame = CGRectMake(0, SCREENH, SCREENW, AH + 20);
    self.backgroundColor = [UIColor clearColor];
    
    //  上面的背景
    UIView *topL = [[UIView alloc]init];
    topL.layer.cornerRadius = 15;
    topL.layer.masksToBounds = YES;
    topL.backgroundColor = [UIColor whiteColor];
    topL.frame = CGRectMake(16, 0, SCREENW - 32, TH);
    [self addSubview:topL];


    UIView *lineTop = [[UIView alloc]init];
    lineTop.frame = CGRectMake(0, TH / (chooss.count + 1), topL.frame.size.width, 0.5);
    lineTop.backgroundColor = [UIColor lightGrayColor];
    [topL addSubview:lineTop];
    
    UILabel *title = [[UILabel alloc]init];
    title.font = HvrStudioChaganTig_M(17);
    title.transform = MenkTransform;
    title.numberOfLines = 0;
    title.textColor = [UIColor lightGrayColor];
    title.text = message;
    CGFloat titleH = TH / (chooss.count + 1);
    title.frame = CGRectMake(0, 0, SCREENW - 32, titleH);
    [title sizeToFit];
    [topL addSubview:title];
    
    
    for (int i = 0; i < chooss.count; i++) {
        UILabel *camera = [[UILabel alloc]init];
        camera.tag = 3000 + i;
        camera.font = HvrStudioChaganTig_M(18);
        camera.transform = MenkTransform;
        camera.numberOfLines = 0;
        camera.textColor = kSCOLOR;
        camera.text = chooss[i];
        camera.textAlignment = NSTextAlignmentCenter;
        camera.frame = CGRectMake(0, titleH + titleH * i, SCREENW - 32, titleH);
        NSLog(@"%@",NSStringFromCGRect(camera.frame));
//        camera.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255) / 255.0 green:arc4random_uniform(255) / 255.0 blue:arc4random_uniform(255) / 255.0 alpha:1];
        [topL addSubview:camera];
        
        camera.userInteractionEnabled = YES;
        UITapGestureRecognizer *labelTapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
        [camera addGestureRecognizer:labelTapGestureRecognizer1];
        
        UIView *lineTop = [[UIView alloc]init];
        lineTop.frame = CGRectMake(0, CGRectGetMaxY(camera.frame), topL.frame.size.width, 0.5);
        lineTop.backgroundColor = [UIColor lightGrayColor];
        [topL addSubview:lineTop];
        
        
    }
    
    UILabel *cancel = [[UILabel alloc]init];
    cancel.layer.cornerRadius = 15;
    cancel.layer.masksToBounds = YES;
    cancel.transform = MenkTransform;
    cancel.font = HvrStudioChaganTig_M(18);
    cancel.textAlignment = NSTextAlignmentCenter;
    cancel.backgroundColor = [UIColor whiteColor];
    cancel.text = @"ᠠᠷᠢᠯᠭᠠᠬᠤ";
    cancel.textColor = [UIColor lightGrayColor];
    cancel.frame = CGRectMake(16, TH + 20, SCREENW - 32, titleH);
    cancel.tag = 9;
    [self addSubview:cancel];
    
    [view addSubview:self];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = CGRectMake(0, SCREENH - (AH + 20) - (IS_IPHONE_X ? 34 : 0), SCREENW, (AH + 20));
    }];
    
    

    cancel.userInteractionEnabled = YES;
    UITapGestureRecognizer *labelTapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
    [cancel addGestureRecognizer:labelTapGestureRecognizer2];

}

- (void)click:(UIGestureRecognizer *)ges{
    UILabel *label = (UILabel *)ges.view;
    
    if (label.tag > 9) {
        self.chClick(label.tag - 3000);
    }else{
        self.chClick(9);
    }
    
    [self hiddenSelf];
    
}

- (void)hiddenSelf{
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = CGRectMake(0, SCREENH , SCREENW, 280);
    } completion:^(BOOL finished) {
        [self.shawdL removeFromSuperview];
        [self removeFromSuperview];
    }];
}
@end
